<?php
/**
 * Created by PhpStorm.
 * User: Guangpen Chen
 * Date: 2014-11-20
 * Time: 15:35
 */

include 'header.php';

include_once('/var/www/html/new/com.inc.php');
include_once("/var/www/html/new/d/class/ProductInfo.php");
include_once("/var/www/html/new/d/class/Member.php");
include_once("/var/www/html/new/d/class/SoapInit.class.php");


$soap       = SoapInit::GetSoapInside();
$isLogin    = !empty($_SESSION) && $_SESSION['memberID']> 0 ? 1: 0;
$memberID   = $_SESSION['memberID'];


$pid      = abs($_REQUEST['pid']);
$aid      = abs($_REQUEST['aid']);
$p        = new ProductInfo($soap, $pid, $aid);
$ptypes   = ProductInfo::$ptype;
$p_info   = $p->pInfo();//返回产品信息

//print_r($p_info);


$p->saleCheck();//查看产品是否在售
$begintime = $p->getMinValidTime($begintime);/*产品有效起始日期*/
if($begintime==-1) {
    exit("无有效出行日期，请更换产品");
}
//TODO::如果未登录或分销商无购买权限，那么使用最高的零售价
if(!$isLogin OR !$p->supply($memberID) OR $_SESSION['dtype']==5){//查看产品是否供应供应给该分销商
    $uprice     = $p->uPrice($memberID, $begintime);//返回产品零售价
    $jsprice    = $uprice;
}
else{
    //分销商id
    $jsprice    = $p->jsPrice($memberID, $begintime);//返回产品分销价
    $uprice     = $p->uPrice($memberID,  $begintime);//返回产品零售价
}
$IdentityFlag = false;//分销商身份标识
$accountMoney = 0;//会员账户余额
$creditMoney  = 0;//可用授信额度

if ($isLogin) {
    $memObj  = new Member($soap, $memberID);
    $accountMoney  = $memObj->money() / 100;//查询会员账户余额
    $TopContacts = $memObj->cinfos(-1);//查询常用联系人
    if ($_SESSION['dtype']<2) {
        $IdentityFlag = true;
        $creditMoney = $memObj->tmoney($p_info['sapply_did']) / 100;//查询会员账户可用授信额度
    }
//    print_r($_SESSION);
}
$buynum         = $p_info['buy_limit_low']?$p_info['buy_limit_low']:1;
//附属产品
$products=array();
$products[]=array(
    "pid"       =>$pid,
    "id"        => "purchaseNum",
    "name"      => "tnum",
    "ttitle"    => $p_info['ttitle'] . ($p_info['v_time_limit']==0 ? '' :
            '(<span style="color:#F33">验票时间'.str_replace('|', '至', $p_info['v_time_limit']).'</span>)'),
    "buynum"    => $buynum,
    "jsprice"   => $jsprice / 100,
    "uprice"    => $uprice / 100,
    "tprice"    => $p_info['tprice'] * 100 / 100,
    "minTime"   => $begintime,
    "ddays"     => $p_info['ddays'],
    "buy_limit_low" =>$p_info['buy_limit_low'],
    'buy_limit_up'  =>$p_info['buy_limit_up'],
    'pay'       => $p_info['pay'],
    'note'      => $p_info['getaddr'],
);
$payMethod =  $p_info['pay'];

if ( $p_info['p_type']!="F" && $p_info['v_time_limit']==0 && $p_info['sync_status']!=2) {
    //TODO::已登陆的分销商或供应商
    if ($IdentityFlag) {
        $c_pros=$memObj->salePros($p_info['lid']);//查询会员可分销产品信息
        foreach($c_pros as $row){
            if($row['sapply_did']!=$_REQUEST['aid'] && $row['apply_did']!=$memberID) {
                continue;
            }
            $c_pids[]=$row['pid'];
        }
        if (count($c_pids)) {
            foreach($c_pids as $c_pid){
                if($c_pid==$p_info['pid']) continue;
                $apro=new ProductInfo($soap,$c_pid,$aid);
                try{
                    $c_jsprice  = $apro->jsPrice($memberID,$begintime);
                    $c_uprice   = $apro->uPrice($memberID,$begintime);
                    $c_pro      = $apro->pInfo();
                    if($c_pro['pay'] != $payMethod || $c_pro['v_time_limit'] != 0 || $c_pro['sync_status']==2) continue;//验票时间是否不限
                    $time    = strtotime($begintime)+($c_pro['ddays']-$p_info['ddays'])*24*3600;
                    $minTime = date("Y-m-d", $time);
                    $products[]=array(
                        "pid"       => $c_pro['pid'],
                        "id"        => $c_pro['id'],
                        "name"      => "c_pids[".$c_pid."]",
                        "ttitle"    => $c_pro['ttitle'],
                        "buynum"    => 0,
                        "jsprice"   => $c_jsprice / 100,
                        "uprice"    => $c_uprice / 100,
                        "tprice"    => $c_pro['tprice']*100 / 100,
                        "minTime"   => $minTime,
                        "buy_limit_low" => $c_pro['buy_limit_low'],
                        'buy_limit_up'  => $c_pro['buy_limit_up'],
                        'note'      => $c_pro['getaddr'],
                    );
                } catch(Exception $e) {
                    //结算价未设置
                }
            }
        }
    }
    else {//散客，未登录
        $sqlTickets = <<<SQL
SELECT t.id as tid,t.title,t.pid,t.tprice,t.apply_did as aid,t.pay,t.getaddr,
t.notes as note,t.delaydays,t.ddays,t.buy_limit_low,t.buy_limit_up,t.sync_status,
f.dhour,f.v_time_limit FROM uu_jq_ticket t
LEFT JOIN uu_land_f f ON t.id=f.tid
WHERE t.landid={$p_info['lid']} AND t.apply_did={$p_info['sapply_did']}
SQL;
        $GLOBALS['le']->query($sqlTickets);
        while ($row=$GLOBALS['le']->fetch_assoc()) {
            //验票时间是否不限，是否为同步的票,支付方式是否一致
            if ($row['pid']==$pid || $row['pay']!=$payMethod ||
                $row['v_time_limit'] != 0 || $row['sync_status']==2) continue;
            $uprice = $soap->get_Dynamic_Price_Merge($row['pid'],$begintime,1,"","",1); //1单个价格
            if($uprice==-1){
                continue;
//                $uprice = $soap->get_Dynamic_Price_Merge($row['pid'],$begintime,2,"","",1);//2单个最低价
            }
            $time    = strtotime($begintime)+($row['ddays']-$p_info['ddays'])*24*3600;
            $minTime = date("Y-m-d", $time);
            //if ($uprice==-1) continue;
            $products[] = array(
                "id"        => $row['tid'],
                "pid"       => $row['pid'],
                "name"      => "c_pids[".$row['pid']."]",
                "ttitle"    => $row['title'],
                "buynum"    => 0,
                "jsprice"   => $uprice / 100,
                "uprice"    => $uprice / 100,
                "tprice"    => $row['tprice']/100*100,
                "minTime"   => $minTime,
                "buy_limit_low" =>$row['buy_limit_low'],
                'buy_limit_up'  =>$row['buy_limit_up'],
                'note'  =>$row['getaddr'],
            );
        }
    }
}
//供应商信息
$str="select dname,qq,mobile,cname from pft_member where id=".$p_info['sapply_did']." limit 1";
$GLOBALS['le']->query($str);
$a_info=$GLOBALS['le']->fetch_assoc();
//产品说明
$timeTitle  = ($p_info['ddays']==0)?"有效期内，可预订当天门票":"有效期内，需提前".($p_info['ddays'])."天预订";
$unitT      = $p_info['p_type']=="C"?"间":"张";
$tips       = $p_info['ddays']>0?"需提前".$p_info['ddays']."天预订，":"可预订当日票，";
$tips       .= $p_info['buy_limit_low']>0?$p_info['buy_limit_low'].$unitT."起订，":"1".$unitT."起订，";
if($p_info['p_type']=="A"){
    $tips   .= ($p_info['delaydays']>0)?"游玩日期起".$p_info['delaydays']."天内有效，":"游玩日期当日有效，";
}
$tips.=($p_info['status']!=0)?"<i>".($p_info['mdays']!=0?"自动":"人工")."确认</i>后生效，":"";
$tips=$tips.$p_info['notes'];



//设置支付方式
if ( $creditMoney > $jsprice*$tnum ){
    $paymodeCheck0="";
    $paymodeCheck1="";
    $paymodeCheck2="checked";
    $default_pay_mode = 2;
}
elseif ( $accountMoney > $jsprice*$tnum ){
    $paymodeCheck0="checked";
    $paymodeCheck1="";
    $paymodeCheck2="";
    $default_pay_mode = 0;
}
else {
    $paymodeCheck0="";
    $paymodeCheck1="checked";
    $paymodeCheck2="";
    $default_pay_mode = 1;
}

//print_r($_SESSION);

//include 'pmakeorder.html';
//print_r($p_info);
//print_r($products);
?>
<link href="css/piao.css" rel="stylesheet" />
<link type="text/css" rel="stylesheet" href="http://www.12301.cc/css/ui/Calendar.css" media="screen" />
<div class="process">
    <span class="process1">1、选择景区门票类型</span>
    <span class="process2">2、填写订单信息</span>
    <span class="process3">3、完成支付预定成功</span>
</div>
<div class="buy-ticket-main">
    <input type="hidden" id="remaindMoney" value="<?=$accountMoney?>"/>
    <input type="hidden" id="remainTmoney" value="<?=$creditMoney?>"/>
    <input type="hidden" id="minbuytnum" value="<?=$p_info['buy_limit_low']?>"/>
    <input type="hidden" id="pay" value="<?=$p_info['pay']?>"/>
    <input type="hidden" id="tourist_info" value="<?=$land_f_info['tourist_info']?>"/>
    <form action="formSubmit.php" method="post" name="group">
        <input type="hidden" name="pid" id="pid" value="<?=$pid?>"/>
        <input type="hidden" name="aid" id="aid" value="<?=$aid?>"/>
        <div class="buy-ticket-main-left left">
            <h2><?=$p_info['ltitle']?></h2>
            <div class="buy-ticket-notice">
                <h4><span>预订数量</span></h4>
                <div class="piao-type-list">
                    <table cellpadding="0" cellspacing="0" border="0" id="piaolist">
                        <thead>
                            <tr>
                                <td class="td1">门票类型</td>
                                <td class="td2">市场价</td>
                                <td class="td3">优惠价</td>
                                <td class="td4">支付方式</td>
                                <td class="td5">购票数量</td>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach($products as $row):?>
                            <tr>
                                <td class="list_1 list"><?=$row['ttitle']?></td>
                                <td class="list_2 list t-oriprice">¥<?=$row['tprice']?></td>
                                <td class="list_3 list t-price"><em class="price jprice tprice_<?=$row['pid']?>">¥<?=$row['jsprice']?></em></td>
                                <td class="list_4 list"><?=$row['pay']==1? "在线支付" : "现场支付"?></td>
                                <td class="list_5 list">
                                    <input type="hidden" class="tprice_<?=$row['pid']?>" value="<?=$row['jsprice']?>"/>
                                    <div class="jiajian">
                                        <a href="javascript:;" class="copies_cut minus jian jjBtn J_jianBtn minus"></a>
                                        <input type="text" id="<?=$row['id']?>" name="<?=$row['name']?>" value="<?=$row['buynum']?>" p_minTime="<?=$row['minTime']?>" p_minbuy="<?=$row['buy_limit_low']?>" class="copies_num input_text tnum">
                                        <a href="javascript:;" class="copies_add add jia jjBtn J_jiaBtn add"></a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach;?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="buy-ticket-notice">
                <h4><span>订票信息</span></h4>
                <div class="buy-ticket-info-con" id="reserveForm">
                    <table width="100%" border="0" cellspacing="2">
                            <tbody>
                                <tr>
                                    <td width="88" align="center">
                                        <label for="ouser">姓 名 :</label></td>
                                    <td width="537">
                                        <input type="text" class="ipt" id="ordername" autocomplete="off" name="ordername"  placeholder="请输入您的真实姓名" required="required">
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        <label for="otel">手 机 :</label></td>
                                    <td>
                                        <input type="tel" class="ipt <?=$isLogin? 'lg':''?> " maxlength="11" onkeyup="value=value.replace(/\D+/g,&#39;&#39;)" name="ordertel" id="ordertel" placeholder="输入手机号方便我们联系您">
                                    </td>
                                </tr>  
                                <tr>
                                    <td align="center">
                                        <label for="odate">日 期 :</label></td>
                                    <td>
                                        <!-- <input type="text" class="ipt odate inputText" name="begintime" id="begintime"  size="16" > -->
                                         <input type="text" name="begintime" id="begintime" class="begintime ipt odate" value="<?=$begintime?>" />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        <label for="OrderLen">总价</label></td>
                                    <td>
                                       <span class="total_num">¥<em role="priceTotal" class="subtotal" id="summoney"><?=$buynum*$jsprice/100?></em></span>
                                    </td>
                                </tr>
                               <!--  <tr>
                                    <td align="center">
                                        <label for="submit_btn"></label>
                                    </td>
                                    <td>
                                        <input type="button" name="submit_btn" class="submit_btn submitBtn" value="提交订单">
                                    </td>
                                </tr> -->
								
                            </tbody>
                        </table>
                </div>
            </div>
			<div class="buy-ticket-notice">
                <h4><span>支付方式</span></h4>
				<div class="box">
					<div class="line pmodel" style="padding-left:20px;">
						<?php if($payMethod==0):?>
							现场支付
						<?php else:?>
							<?php if($IdentityFlag):?>
								<?php if($p_info['apply_did']==$apply_did):?>
									预订自供产品，无需支付
								<?php else:?>
									<? if($creditMoney>0): ?>
									<div class="">
										<input class="paymode" <?=$paymodeCheck2?> type="radio" name="paymode" value="2">
										<span class="credit">授信余额<label> (可用<em class="money">¥<?=$creditMoney?></em>)</label></span>
									</div>
									<? endif;?>
									<? if($accountMoney>0): ?>
									<div class="">
										<input class="paymode" <?=$paymodeCheck0?> type="radio" value="0" name="paymode">
										<span>账户余额<label> (可用<em class="money">¥<?=$accountMoney?></em>)</label></span>
									</div>
									<? endif; ?>
									<div class="">
										<input class="paymode" <?=$paymodeCheck1?> type="radio" value="1" name="paymode">
										<span>在线支付</span>
									</div>
								<?php endif;?>
							<?php else:?>
								<input class="paymode" <?=$paymodeCheck1?> type="radio" value="1" name="paymode">
								<span>在线支付</span>
							<?php endif;?>
						<?php endif;?>
					</div>
				</div>	
				<tr>
					<td align="center">
						<label for="submit_btn"></label>
					</td>
					<td>
						<input type="button" name="submit_btn" class="submit_btn submitBtn" value="提交订单">
					</td>
                </tr>
			</div>
            <div class="buy-ticket-notice">
                <h4><span>订票须知</span></h4>
                <div class="buy-ticket-notice-con" style="height: auto;">
                    <h3>购票需知：</h3>
                    <?=nl2br($p_info['jqts'])?>
                    <h3>产品说明：</h3>
                    <?=$tips?>
                    <h3>取票信息：</h3>
                    <?=$p_info['getaddr']?>
                </div>
            </div>
        </div>
		<!-- <a href="javascript:showBg();">登陆</a> -->
    </form>
        <div class="buy-ticket-main-right right">
            <div id="right" style="top: 0px;">
                <h2 class="main-con-right-title">为您推荐</h2>
                <ul>
                    <li>
                        <dl>
                            <dt><a href="#" title=""><img src="#" width="250" height="151" alt=""></a></dt>
                            <dd class="tuijian-title">【特惠】欢乐谷夜场（含水公园门票）  原价80网上预订59元[景区支付]</dd>
                            <dd class="tuijian-price">￥59<span>市场价:￥80</span></dd>
                            <dd class="tuijian-btn"><a href="#" title=""></a></dd>
                        </dl>
                    </li>
                    <li style="border:none;">
                        <dl>
                            <dt><a title="" href="#"><img src="#" width="250" height="151" alt=""></a></dt>
                            <dd class="tuijian-title">《极力道》门票(周二-周日19:40场)480网上预订360元[景区支付]</dd>
                            <dd class="tuijian-price">￥360<span>市场价:￥480</span></dd>
                            <dd class="tuijian-btn"><a href="#" title=""></a></dd>
                        </dl>
                    </li>
                </ul>
            </div> 
        </div>
        <div style="clear: both"></div>
</div>
<?php if(!$isLogin):?>
<div id="fullbg"></div>
<div id="dialog">
    <p class="close">
        <a href="#" onclick="closeBg();">关闭</a>
    <div>
        <div class="member_login_a">
            <div class="login_con">
                <form method="post" id="myform" name="myform">
                    <table cellspacing="10">
                        <tbody>
                        <tr>
                            <td>
                                <label>账户名</label></td>
                            <td>
                                <input type="text" id="username" name="username" size="22" onfocus="iC(this)" class="input-txt" value="用户名/手机号码" autocomplete="off"></td>
                        </tr>
                        <tr>
                            <td>
                                <label>密&nbsp;&nbsp;码</label></td>
                            <td>
                                <input type="password" id="pwd" name="pwd" size="22" class="input-txt" onfocus="iC(this)" value=""></td>
                        </tr>
                        <tr>
                            <td align="right"></td>
                            <td>
                                <div class="btn_con">
                                    <input type="button" name="dosubmit" id="dosubmit" value="登录" class="reg_btn"></div>
                                <span class="free_reg"><a href="">免费注册</a> | <a href="">找回密码</a></span>
                            </td>
                        </tr>
                        </tbody></table>
                </form>
            </div>
        </div>
    </div>
</div>
<?php endif;?>
<script type="text/javascript" src="//www.12301.cc/js/ui/PopWindow.js"></script>
<script type="text/javascript" src="//www.12301.cc/js/md5.js"></script>
<script type="text/javascript" src="js/Calendar.js"></script>
<script type="text/javascript" src="js/productOrder.js"></script>

<div id="footer">
            <div class="footer-con">
                <div class="footer-con-center "></div>
                <div class="footer-con-right">
                   <p>24小时服务热线：0591-12301</p> 
                </div>
            </div>
       </div>
<script>
    (function(){
        $("#dosubmit").on('click', function(){
            var userInp = $("#username");
            var pswdInp = $("#pwd");
            var username = userInp.val();
            var pasd = pswdInp.val();
            if(username.length==0){
                alert("请输入帐号");
                return false;
            }
            if(!pasd){
                alert("请输入密码");
                return false;
            }
            $.post('/wx/api/login.php', {
                account : username,
                password : hex_md5(pasd)}, function(res){
                if(res && res.status == "ok"){ //登录成功
                    closeBg();
                    location.reload();
                }else{
                    alert(res.msg);
                }
            },'json');
        });
    }());
	function showBg() { 
			var bh = $("body").height();
			var bw = $("body").width(); 
			$("#fullbg").css({ 
				height:bh, 
				width:bw, 
				display:"block" 
			}); 
			$("#dialog").show(); 
		} 
		function closeBg() { 
			$("#fullbg,#dialog").hide(); 
		} 
		
		 function iC(ipt) {
            ipt.onfocus = function () {
                if (this.value == this.defaultValue) { this.value = ''; this.style.color = '#000'; }
                this.style.border = "1px solid #008eff";
                this.style.boxShadow = "0 0 2px #008eff";
                this.style.transition = "all 0.2s ease-out"
            };
            ipt.onblur = function () {
                if (this.value == '') { this.value = this.defaultValue; this.style.color = '#999'; }
                this.style.border = "1px solid #ccc";
                this.style.boxShadow = "0 0 0px #fff";
            };
            ipt.onfocus();
        }
</script>

</body>
</html>
